package com.jrx.anytxn.transaction.job.partnerGeneralFile.config;

import com.jrx.anytxn.common.exception.TxnBizException;
import com.jrx.anytxn.param.entity.PrProductTable;
import com.jrx.anytxn.param.service.product.IProductTableService;
import com.jrx.anytxn.transaction.config.PartnerGeneralFileConfig;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.job.partnerGeneralFile.step.PartnerGeneralFileListener;
import com.jrx.anytxn.transaction.service.IPartnerCountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 产品当日放还款统计文件
 */
@Configuration
@EnableConfigurationProperties(PartnerGeneralFileConfig.class)
public class PartnerGeneralFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(PartnerGeneralFileConfiguration.class);

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private IPartnerCountService partnerGeneralService;
    @Autowired
    private PartnerGeneralFileConfig partnerGeneralFileConfig;
    @Autowired
    private PartnerGeneralFileListener partnerGeneralFileListener;
    @Autowired
    private IProductTableService productTableService;

    @Bean
    public Job partnerGeneralFileJob() {
        return jobBuilderFactory.get("partnerGeneralFileJob")
                .listener(partnerGeneralFileListener)
                .start(partnerGeneralStep())
                .build();
    }

    @Bean
    public Step partnerGeneralStep() {
        return stepBuilderFactory.get("partnerGeneralStep")
                .tasklet(partnerGeneralTasklet())
                .build();
    }

    @Bean
    @StepScope
    public Tasklet partnerGeneralTasklet() {
        return (stepContribution, chunkContext) -> {
            List<PrProductTable> productTableList = productTableService.findAllForBatch(TransBizConstant.STATUS_1);
            try {
                partnerGeneralService.partnerGeneral(productTableList,partnerGeneralFileConfig.getGeneralPath(),
                        partnerGeneralFileConfig.getGeneralName(),TransBizConstant.GENERAL_FILE_TYPE);
            } catch (TxnBizException e) {
                logger.error("产品放款还款统计异常：",e);
            }
            logger.info("放款还款统计文件生成");
            return RepeatStatus.FINISHED;
        };
    }

}
